//
//  EquipmentViewController.m
//  LOL_PalmarHelper
//
//  Created by dllo on 15/6/19.
//  Copyright (c) 2015年 hantao. All rights reserved.
//

#import "EquipmentViewController.h"

@interface EquipmentViewController ()

@end

@implementation EquipmentViewController


-(void)dealloc
{
    [_allEquipmentArray release];
    [_myCollectV release];
    [super dealloc];


}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = @"装备";
    
    self.allEquipmentArray = [NSMutableArray array];
    
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.minimumInteritemSpacing = 5;
    flowLayout.sectionInset = UIEdgeInsetsMake(30, 10, 10, 10);
    flowLayout.itemSize = CGSizeMake((self.view.bounds.size.width - 20 - 15) / 4, ((self.view.bounds.size.width - 20 - 15) / 4) + 25);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    
    self.myCollectV = [[UICollectionView alloc] initWithFrame:CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, self.view.bounds.size.width, self.view.bounds.size.height - 64 - 49) collectionViewLayout:flowLayout];
    self.myCollectV.backgroundColor = [UIColor whiteColor];
    
    self.myCollectV.delegate = self;
    self.myCollectV.dataSource = self;
    
    [self.myCollectV registerClass:[EquipmentCollectionViewCell class] forCellWithReuseIdentifier:@"Reuse"];
    
    [self.view addSubview:self.myCollectV];
    [_myCollectV release];
    
    
    [self requestDate];
    
    
    
    
}



-(void)requestDate
{
    
    MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
    HUD.labelText = @"HHZ小组正卖力加载中";
    [self.navigationController.view addSubview:HUD];
    [HUD show:YES];
    [HUD release];
    

    [[HeroBaseHandle ShareHeroHeroDate] openDB];

    if ([[HeroBaseHandle ShareHeroHeroDate] selectEquipment] != nil) {
        
        self.allEquipmentArray = [[HeroBaseHandle ShareHeroHeroDate] selectEquipment];
        
    }
    

    [AFN AFNGetData:@"http://lolbox.duowan.com/phone/apiZBItemList.php?tag=all" andBlock:^(id respondes) {
        
        
        
        
        if (![respondes isKindOfClass:[@"error" class]]) {
            
            [[HeroBaseHandle ShareHeroHeroDate] openDB];
            [[HeroBaseHandle ShareHeroHeroDate] dropEquipment];
            [self.allEquipmentArray removeAllObjects];
            
            [[HeroBaseHandle ShareHeroHeroDate] createEquipmentTable];
            
            NSMutableArray *array = respondes;
            
            for (NSMutableDictionary *dic in array) {
                
                EquipmentModel *equipment = [[EquipmentModel alloc] init];
                
                equipment.equipentId = [dic objectForKey:@"id"];
                equipment.text = [dic objectForKey:@"text"];
                
                [self.allEquipmentArray addObject:equipment];

                [[HeroBaseHandle ShareHeroHeroDate] insertEquipment:equipment];
                [equipment release];
                
            }
            
            [HUD hide:YES];
            [self.myCollectV reloadData];
                  
        }else if ([respondes isEqualToString:@"error"]) {
            [HUD hide:YES];
            
            
            UIAlertView *al = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络不给力" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [self.view addSubview:al];
            [al show];
            [al release];
            
            
            
        }
        
        
    }];

    

}



-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

    EquipmentDetailViewController *equipmentDetail = [[EquipmentDetailViewController alloc] init];
    
   EquipmentModel *model = [self.allEquipmentArray objectAtIndex:indexPath.row];
    
    equipmentDetail.equipmentID = model.equipentId;
    
    [self.navigationController pushViewController:equipmentDetail animated:YES];
    


}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{

    return self.allEquipmentArray.count;
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{

    return 1;
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    EquipmentCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Reuse" forIndexPath:indexPath];
    cell.layer.transform = CATransform3DMakeScale(0.3, 0.3, 0.1);
    [UIView animateWithDuration:0.5 animations:^{
        
        cell.layer.transform = CATransform3DMakeScale(1, 1, 0.1);
        
        
    }];
    
   EquipmentModel *equipment = [self.allEquipmentArray objectAtIndex:indexPath.row];
    
    cell.equopment = equipment;
    
    cell.backgroundColor = [UIColor whiteColor];
    
    
    return cell;


    


}

    
    


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
